<?php
/**
 * @title 18. 四数之和 - 剪枝
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[][]
     */
    function fourSum($nums, $target) {
        /**
         * @since 2020-10-14 排序
         */
        sort($nums);
        // echo "nums=[", implode(",", $nums), "]",PHP_EOL;

        /**
         * @since 2020-10-14 数组长度
         */
        $len = count($nums);

        /**
         * @since 2020-10-15 结果集
         */
        $result = [];

        /**
         * @since 2020-10-15 最后4位, 3位, 2位之和, 最开始4位之和
         */
        $max21 = $nums[$len-2] + $nums[$len-1];
        $max321 = $nums[$len-3] + $max21;
        $max4321 = $nums[$len-4] + $max321;
        $min1234 = $nums[0] + $nums[1] + $nums[2] + $nums[3];
        // echo "max321={$max321}, max21={$max21}", PHP_EOL, PHP_EOL;

        /**
         * @since 2020-10-15 最大4个数都无法达到直接返回
         */
        if($max4321 < $target OR $min1234 > $target){
            return [];
        } else {}

        /**
         * @since 2020-10-14 双循环
         */
        $lastI = null;
        for($i=0; $i<=$len-4; $i++){
            // echo "i=nums[{$i}] = ", $nums[$i];
            /**
             * @since 2020-10-15 小于最小值, break
             */
            if($nums[$i] + $nums[$i+1] + $nums[$i+2] + $nums[$i+3] > $target){
                // echo "break", PHP_EOL;
                break;
            } else {}

            /**
             * @since 2020-10-15 大于最大值, continue
             */
            if($nums[$i] + $max321 < $target){
                // echo "continue", PHP_EOL;
                continue;
            } else {}
            // echo PHP_EOL;

            /**
             * @since 2020-10-14 防止重复
             */
            if($nums[$i] === $lastI){
                continue;
            } else {
                $lastI = $nums[$i];
            }

            $lastJ = null;
            for($j=$i+1; $j<=$len-3; $j++){
                // echo "--j=nums[{$j}]=", $nums[$j];

                /**
                 * @since 2020-10-15 小于最小值, break
                 */
                if($nums[$i] + $nums[$j] + $nums[$j+1] + $nums[$j+2] > $target){
                    // echo ", break", PHP_EOL;
                    break;
                } else {}

                /**
                 * @since 2020-10-15 大于最大值, continue
                 */
                if($nums[$i] + $nums[$j] + $max21 < $target){
                    // echo ", continue", PHP_EOL;
                    continue;
                } else {}
                // echo PHP_EOL;

                /**
                 * @since 2020-10-14 防止重复
                 */
                if($nums[$j] === $lastJ){
                    continue;
                } else {
                    $lastJ = $nums[$j];
                }
                // echo "nums[{$i}]=", $nums[$i], ", nums[{$j}]=", $nums[$j], PHP_EOL;

                /**
                 * @since 2020-10-15 左右指针
                 */
                $left = $j+1;
                $right = $len-1;
                while($left<$right){
                    $val = $nums[$left]+$nums[$right] + $nums[$i]+$nums[$j];

                    /**
                     * @since 2020-10-15 大了, right向左移
                     */
                    if($val > $target){
                        $right--;
                    } elseif($val < $target) {
                        /**
                         * @since 2020-10-15 小了, left向左移
                         */
                        $left++;
                    } else {
                        /**
                         * @since 2020-10-15 等于, 找到目标值
                         */
                        $result[] = [$nums[$i], $nums[$j], $nums[$left], $nums[$right]];

                        /**
                         * @since 2020-10-15 左右指针继续向中间移动, 需要找到不同的值, 避免重复
                         */
                        $left++;
                        $right--;
                        while($left<$right){
                            if($nums[$left] != $nums[$left-1]){
                                break;
                            } else {}
                            $left++;
                        }
                        while($left<$right){
                            if($nums[$right] != $nums[$right+1]){
                                break;
                            }
                            $right--;
                        }
                    }
                }
            }
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.05.php
 */
$datas = [
    [[1, 0, -1, 0, -2, 2], 0],
    // [[-1,0,1,2,-1,-4], -1],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->fourSum(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
